package cz.hackathon;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RatingBar.OnRatingBarChangeListener;
import cz.hackathon.rateit.model.Item;
import cz.hackathon.rateit.model.Rating;

public class RatingActivity extends Activity implements OnItemClickListener,
		OnRatingBarChangeListener {

	private Item item;
	private Rating activeRating;
	private int newRating;
	private String newDescription;

	private RatingAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.rating_list);

		item = (Item) getIntent().getSerializableExtra("Item");

		ListView ratingList = (ListView) findViewById(R.id.list_ratings);

		adapter = new RatingAdapter(getBaseContext(), getRatings());
		ratingList.setAdapter(adapter);

		ratingList.setOnItemClickListener(this);
	}

	private void notifyChange() {
		adapter.notifyDataSetChanged();
	}

	private class RatingAdapter extends ArrayAdapter<Rating> {

		public RatingAdapter(Context context, List<Rating> objects) {
			super(context, R.layout.rating_single, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RelativeLayout clientView;

			Rating rating = getItem(position);

			if (convertView == null) {
				clientView = new RelativeLayout(getContext());
				getLayoutInflater().inflate(R.layout.rating_single, clientView,
						true);
			} else {
				clientView = (RelativeLayout) convertView;
			}

			((TextView) clientView.findViewById(R.id.tv_ratingItem))
					.setText(rating.getName());
			((TextView) clientView.findViewById(R.id.tv_ratingDescription))
					.setText(rating.getDescription());
			if (rating.getValue() != null)
				((RatingBar) clientView.findViewById(R.id.ratingBar_small))
						.setRating(rating.getValue());

			return clientView;
		}

	}

	private List<Rating> getRatings() {
		return item.getRatings();
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		activeRating = (Rating) parent.getItemAtPosition(position);
		showRatingDialog(activeRating);
	}

	private void showRatingDialog(Rating rating) {
		AlertDialog.Builder ad = new Builder(RatingActivity.this);

		ad.setTitle(getString(R.string.make_ratings));

		LinearLayout dialogLayout = new LinearLayout(this.getBaseContext());
		getLayoutInflater().inflate(R.layout.dialog_ratings, dialogLayout);

		TextView ratingName = (TextView) dialogLayout
				.findViewById(R.id.tv_rating_dialog_info);
		ratingName.setText(rating.getName());

		RatingBar ratingBar = (RatingBar) dialogLayout
				.findViewById(R.id.ratingBar_dialog);
		ratingBar.setOnRatingBarChangeListener(this);
		if (activeRating.getValue() != null)
			ratingBar.setRating(activeRating.getValue());

		EditText editDescriptionText = (EditText) dialogLayout
				.findViewById(R.id.et_dialogDescription);
		editDescriptionText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				newDescription = s.toString();
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});
		if (activeRating.getDescription() != null)
			editDescriptionText.setText(activeRating.getDescription());

		ad.setView(dialogLayout);
		ad.setPositiveButton(R.string.okay,
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						activeRating.setValue(newRating);
						activeRating.setDescription(newDescription);
						notifyChange();
					}
				});
		ad.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		ad.setCancelable(true);
		ad.show();
	}

	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		newRating = (int) (rating + 0.5);
	}

}
